<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/header.jsp" %>
<%@ include file="/common/datatables.jsp" %>
<script type="text/javascript" src="${ctx}/assets/js/plupload/plupload.full.min.js"></script>     
<script type="text/javascript" src="${ctx}/assets/js/plupload/jquery.plupload.queue.min.js"></script>
<script type="text/javascript" src="${ctx}/assets/js/plupload/i18n/zh_CN.js"></script>
<script type="text/javascript" src="${ctx}/assets/js/fnReloadAjax.js"></script>
<div class="row">
	<div class="col-xs-12">	
		<div class="widget-box widget-color-blue2">			
			<div class="widget-header">
				<h5 class="widget-title">客户查询</h5>
				<div class="widget-toolbar">
					<a href="#" data-action="fullscreen" class="orange2"><i class="ace-icon fa fa-expand"></i></a>
					<a href="#" data-action="collapse"><i class="ace-icon fa fa-chevron-up"></i></a>
					<a href="#" data-action="close"><i class="ace-icon fa fa-times"></i></a>
				</div>
			</div>
			<div class="widget-body">
				<div class="widget-main no-padding">				
					<form class="form-horizontal" id="queryCustomerForm">
						<fieldset>
						    <div class="row">
								<div class="form-group col-xs-6">
									<label class="col-sm-3 control-label no-padding-left" for="wtCustName1">客户姓名</label>
									<div class="col-sm-9">
										<div class="clearfix">
												<input type="text" id="wtCustName1" name="wtCustName" class="col-xs-10" value=""/>
										</div>
									</div>
								</div>
								 <div class="form-group col-xs-6">
									<label class="col-sm-3 control-label no-padding-left" for="wtCustCardno1">身份证号码</label>
									<div class="col-sm-9">
										<div class="clearfix">
												<input type="text" id="wtCustCardno1" name="wtCustCardno" class="col-xs-10" value=""/>
										</div>
									</div>
								</div>
							</div>
							<div class="row">
							   <div class="form-group col-xs-4" id="divtime1">
									<label class="col-sm-3 control-label no-padding-left" for="startTime1">开始时间:</label>
									<div class="col-sm-9">
										<div class="clearfix">
											<input type="text" id="startTime" name="startTime" size="20"  style="width:150px;"/>
										</div>
									</div>
								</div>
								<div class="form-group col-xs-4" id="divtime2">
									<label class="col-sm-3 control-label no-padding-left" for="endTime1">结束时间:</label>
									<div class="col-sm-9">
										<div class="clearfix">
											<input type="text" id="endTime" name="endTime" size="20"  style="width:150px;"/>
										</div>
									</div>
								</div>
								<div class="form-group col-xs-4">
									<label class="col-sm-3 control-label no-padding-left" for="orgId">制定机构</label>
									<div class="col-sm-9">
										<div class="clearfix">
											<input type="text" id="orgIdQuery" name="orgId" class="col-xs-10" />
										</div>
									</div>
								</div>
							</div>
							
						</fieldset>
						<div class="form-actions center">
							<button class="btn btn-sm btn-info" type="button" id="searchCustomer">
								<i class="icon-on-right ace-icon fa fa-search bigger-110"></i>
								查询
							</button>
							&nbsp; &nbsp; &nbsp;
							<button class="btn btn-sm btn-purple" type="button" id="addCustomer">
								<i class="ace-icon fa fa-plus-square bigger-110"></i>
								新增
							</button>
							&nbsp; &nbsp; &nbsp;
							<button class="btn btn-sm" type="reset">
								<i class="ace-icon fa fa-undo bigger-110"></i>
								重置
							</button>
							&nbsp; &nbsp; &nbsp;
							<button class="btn btn-sm btn-info" type="button" id="exportTemplate">
								<i class="icon-on-right ace-icon fa fa-search bigger-110"></i>
								模板导出
							</button>
							&nbsp; &nbsp; &nbsp;
							<button class="btn btn-sm btn-purple" type="button" id="uploadDatesource">
								<i class="ace-icon fa fa-cloud-upload bigger-110"></i>
								数据导入
							</button>
							&nbsp; &nbsp; &nbsp;
							<button class="btn btn-sm btn-info" type="button" id="exportDate">
								<i class="icon-on-right ace-icon fa fa-search bigger-110"></i>
									导出
							</button>
						</div>		
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
<div id="errorInfo" style="display:none;" >
	<div class="col-xs-12">
		<div class="widget-box">
			<div class="widget-header widget-header-flat">
				<h4 class="smaller">
					 错误信息
				</h4>
			</div>

			<div class="widget-body">
				<div  class="widget-main">
					<pre id="datasourceInfoList" class="prettyprint linenums">
						</pre>
				</div>
			</div>
		</div>
	</div>
</div>

<div id="customerList"></div>
<div id="customerIframe"></div>

<script type="text/javascript">

var scripts = [null, null];
$('.page-content-area').ace_ajax('loadScripts', scripts, function() {
	
	$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
		_title: function(title) {
			var $title = this.options.title || '&nbsp;'
			if( ("title_html" in this.options) && this.options.title_html == true )
				title.html($title);
			else title.text($title);
		}
	}));
	
	$("#ui-datepicker-div").remove();
	$("#startTime").datepicker({
		showOtherMonths: true,
		selectOtherMonths: true,
		dateFormat:'yy-mm-dd'
	});
	$("#endTime").datepicker({
		showOtherMonths: true,
		selectOtherMonths: true,
		dateFormat:'yy-mm-dd'
	});
	
	//机构/部门选择插件
	$("#orgIdQuery").orgTreeSelect({
			checkType:"org", 
			clickType:"org",
			defaultOrgId:"${fns:getOrgId()}",
			defaultOrgName:'<hw:orgName orgId="${fns:getOrgId()}" />',
			queryRootOrgId:"${fns:getOrgId()}"
		}
	);
	
	$('#queryCustomerForm').validate({
		errorElement: 'div',
		errorClass: 'help-block',
		focusInvalid: false,
		ignore: "",
		rules: {
		},
		messages: {
		},
		submitHandler: function (form) {
		},
		invalidHandler: function (form) {
		}
	});
	
	//新增/修改 dialog
	$('#customerIframe').dialog({
		hide:true,
		autoOpen:false,
		width: 850,
		height:700,
		modal:true,
		title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-cog green'></i>&nbsp;客户管理</h4></div>",
		title_html: true,
		open: function (event,ui) {
			var flag = $(this).data('flag');
			if(flag == "add"){
				$("#btnCustomerSave").show();
				$("#btnCustomerModify").hide();
			}else if(flag == "show"){
				$("#btnCustomerSave").hide();
				$("#btnCustomerModify").show();
			}
        },			
		buttons: [
			{
				id: "btnCustomerModify",
				html: "<i class='ace-icon fa fa-pencil-square-o bigger-110'></i>&nbsp; 修改",
				"class" : "dialog-button-hide btn btn-primary btn-minier",
				click: function() {
					modify();
				}
			},
			{
				id: "btnCustomerSave",
				html: "<i class='ace-icon fa fa-floppy-o bigger-110'></i>&nbsp; 保存",
				"class" : "dialog-button-hide btn btn-success btn-minier",
				click: function() {
					save();
				}
			},
			{
				id: "btnCustomerCancle",
				html: "<i class='ace-icon fa fa-undo bigger-110'></i>&nbsp; 取消",
				"class" : "dialog-button-hide btn btn-warning btn-minier",
				click: function() {
					cancle();
				}
			},												
			{
				id: "btnCustomerClose",
				html: "<i class='ace-icon fa fa-times bigger-110'></i>&nbsp; 关闭",
				"class" : "btn btn-minier",
				click: function() {
					$(this).dialog("close");
					$(this).empty();
				}
			}				
		]				
	});
	
	$("#searchCustomer").click(function(){
		if(!$('#queryCustomerForm').valid())
			return;
		$('#customerList').load("${ctx}/customer/toList.do",$('#queryCustomerForm').serialize());
		$("#customerList").show();
	});
	
	$("#addCustomer").click(function(){
		$('#customerIframe').load("${ctx}/customer/toDetail.do?flag=add",function(){
			$('#customerIframe').data("flag","add").dialog('open');
		});
	});
	
	$("#exportTemplate").click(function(){
		window.location = "${ctx}/customer/downloadTemplate.do?";
	});
	
	$("#exportDate").on("click",function(){
		var startTime = $("#startTime").val();
    	var endTime = $("#endTime").val();
    	if(startTime == null || startTime == "" ){
    		alert("开始时间不能为空");
    		return;		
    	}
    	if(endTime == null || endTime == "" ){
    		alert("结束时间不能为空");
    		return;		
    	}
    	var fromDate = new Date(startTime);
    	var toDate = new Date(endTime);
    	if(toDate < fromDate){
    		alert("结束时间不能小于开始时间");
    		return;
    	}
    	var orgId = $("#orgIdQuery").val();
    	window.location = "${ctx}/report/exportCustomer.do?startTime=" + startTime + "&endTime=" + endTime + "&orgId="+orgId;
	});
	
	$("#uploadDatesource").myfileupload(
		{
			//busiId : 'uploadDatesource', //作为文件的挂靠业务id 必输
			orgId : '${fns:getOrgId()}',
			type : "file",
			filters : "xls,xlsx",
			multiple_queues : false,
			multi_selection : false,
			maxFiles : 1,
			max_file_size : 50,//mb
			myFilesAdded: function (up, files) {
			    $.each(up.files, function (i, file) {
			        if (up.files.length <= 1) {
			            return;
			        }
			        up.removeFile(file);
			    });
			},
			myFileUploaded:function(up, file, res) {
				var response = JSON.parse(res.response);
				$.ajax({
					async : false,
					url : '${ctx}/customer/uploadDatesource.do',
					type : "post",
					data : {
						"fileName" : response.md5Name,
						"filePath" : response.filePath
					},
					dataType : "json",
					success : function(ret) {
						//如果成功
						if (ret.success || ret.success == "true") {
							var errorInfo = "";
							if(ret.errorInfos == null){
								alert("数据解析成功.");
								$('#fileUploadPage').dialog('close');
								//清除错误信息
								$("#datasourceInfoList").empty();
								//刷新
								$("#searchCustomer").click();
							}else{
								var length = 1;
								$.each(
									ret.errorInfos,
									function(i,elem) {
										if(length > 20){//限制展示20条错误信息
											return;
										}
										errorInfo += "<p><span style='color:red'>位置:第"
												  + elem.row
												  + " 行,错误信息:"
												  + elem.msg + "</span>";
										length ++ ;
									});
									//显示错误信息
									$("#datasourceInfoList").html(errorInfo);
									$("#errorInfo").show();
									$('#fileUploadPage').dialog('close');
									alert("数据解析失败.");
									//刷新数据
									$("#searchCustomer").click();
						   }
					   } else {
							alert(ret.msg);
					   }
				 	},
					error : function(ret) {
						alert(ret);
					}
				});
			}
		});
});

function showDetail(pk){
	$('#customerIframe').load("${ctx}/customer/toDetail.do?flag=show&pk="+pk,function(){
		$('#customerIframe').data("flag","show").dialog('open');
	});
}

function modify(pk){
	$('#customerIframe').load("${ctx}/customer/toDetail.do?flag=modify&pk="+pk,function(){
		$('#customerIframe').data("flag","modify").dialog('open');
	});
	
}


function removeItem(pk){
	if(confirm("确定删除该项吗?")){  
      	$.ajax({
			async : false,
			url: "${ctx}/customer/remove.do?pk="+pk,
			type:"post",
			dataType:"json",
			success: function(ret){	
				if (ret[0] == "ok") {
					alert(ret[1]);
					$("#searchCustomer").click();
				} else {
					alert(ret[1]);
				}				
			},
			error: function(ret){
				alert(ret.responseText);
			}
		});
      }  
}

</script>
